/**
 * Copyright (c) 2010-2011 Pierre Labatut
 *
 * This file is part of Space-Chronicles project.
 *
 * Space-Chronicles is free software: you can redistribute it and/or modify it
 * under the terms of its license which may be found in the accompanying
 * LICENSE file or <http://code.google.com/p/space-chronicles/>.
 */

package space.model;

import jinngine.math.Matrix4;

/**
 *
 * @author Pierre
 */
public class RenderingCamera implements Camera{

    final Camera camera;

    public double near() {
        return camera.near();
    }

    public double far() {
        return camera.far();
    }

    public RenderingCamera(Camera camera) {
        this.camera = camera;
    }

    /**
     * {@link http://www.opengl.org/sdk/docs/man/xhtml/gluPerspective.xml}
     * @param viewportWidth
     * @param viewportHeight
     * @return
     */
    public Matrix4 perspective(double viewportWidth, double viewportHeight) {
        double fovy = Math.toRadians(camera.fovy() * .5);
        double f = 1. / Math.tan(fovy);
        double zf = camera.far();
        double zn = camera.near();
        final Matrix4 projection =
                new Matrix4(
                f * viewportHeight / viewportWidth, 0., 0., 0.,
                0., f, 0., 0.,
                0., 0., (zf + zn) / (zn - zf), 2. * zf * zn / (zn - zf),
                0., 0., -1., 0.);
        return projection;
    }

    public Matrix4 viewpoint() {
        return new Matrix4(camera.viewpoint());
    }
 
    public double fovy() {
       return camera.fovy();
    }

    
}
